<?php

namespace App\Admin\Controllers;

use App\ReleaseNeed;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class ReleaseNeedController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('找房需求')
            ->description('找房需求')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('找房需求')
            ->description('找房需求')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('找房需求编辑')
            ->description('找房需求编辑')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('新建找房需求(根据用户电话信息录入)')
            ->description('新建找房需求(根据用户电话信息录入)')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new ReleaseNeed);

        $grid->id('Id');
        $grid->bail_type('委托类型')->display(function ($value) {
            if ($value == 0) {
                return '厂房';
            } else if ($value == 1) {
                return '土地';
            } else {
                return '其他';
            }
        });
        $grid->rent_sale('租售类型')->display(function ($value) {
            if ($value == 0) {
                return '求租';
            } else {
                return '求售';
            }
        });
        $grid->name('委托人');
        $grid->telephone('委托人电话');
        $grid->created_at('创建时间');
        //$grid->desc('委托信息');

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(ReleaseNeed::findOrFail($id));

        $show->id('Id');
        $show->bail_type('委托类型')->as(function ($value) {
            if ($value == 0) {
                return "厂房";
            } else if ($value == 1) {
                return "土地";
            } else {
                return "其他";
            }
        });
        $show->rent_sale('租售类型')->as(function ($value) {
            if ($value == 0) {
                return "求租";
            }else{
                return "求售";
            }
        });
        $show->name('委托人');
        $show->telephone('委托人电话');
        $show->desc('委托信息');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new ReleaseNeed);
        $type = [
            0 => '厂房',
            1 => '土地',
            2 => '其他',
        ];
        $form->select('bail_type', '委托类型')->options($type)->rules('required', [
            'required' => '请选择委托类型'
        ]);
        $rs_type = [
            0 => '求租',
            1 => '求售',
        ];
        $form->select('rent_sale', '租售类型')->options($rs_type)->rules('required', [
            'required' => '请选择租售类型'
        ]);
        $form->text('name', '委托人');
        $form->mobile('telephone', '委托人电话')->options(['mask' => '999 9999 9999']);
        $form->ckeditor('desc', '委托描述');

        return $form;
    }
}
